package org.tooyoung.fourone.live.models;


import android.os.AsyncTask;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

public class MapAsyncTask extends AsyncTask<Void,Room,Void> {
    private List<Room> rooms;
    private Cpi cpi;
    private List<Room> list;

    public MapAsyncTask(Cpi cpi,List<Room> rooms) {
        this.cpi=cpi;
        this.rooms = rooms;
        list=new ArrayList<>();
    }

    @Override
    protected Void doInBackground(Void... voids) {
        int size = rooms.size();
        for (int i = 0; i < size; i++) {
            String city = rooms.get(i).getCity();
            try {
                 city =URLEncoder.encode(city, "GBK");
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
            String str ="http://api.map.baidu.com/cloudgc/v1?ak=xl63sfWNBqzWRX1mGkdoaVKXRT9zkBPd&address="+city+"&city="+city;
            String mapData = getMapData(str);
            try {
                JSONObject jsonObject = new JSONObject(mapData);
                JSONArray result = jsonObject.optJSONArray("result");
                if (result==null)
                {
                 continue;
                }
                else
                {

                    JSONObject jsonObject1 = result.optJSONObject(0);
                    if (jsonObject1!=null) {
                        JSONObject location = jsonObject1.optJSONObject("location");
                        String lat = location.optString("lat");
                        String lng = location.optString("lng");
                        Room room = rooms.get(i);
                        room.setLat(lat);
                        room.setLng(lng);
                        list.add(room);
                    }
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
        return null;
    }
    public String getMapData(String s)
    {
        String str=null;
        try {
            HttpURLConnection connection = (HttpURLConnection) new URL(s).openConnection();
            connection.setRequestMethod("GET");
            connection.setDoInput(true);
            int code = connection.getResponseCode();
            if (code==200)
            {
                InputStream inputStream = connection.getInputStream();
                byte[] bytes = new byte[102400];
                int lenth;
                while ((lenth=inputStream.read(bytes)) != -1) {
                    str = new String(bytes);
                }
            }


        } catch (IOException e) {
            e.printStackTrace();
        }
        return str;
    }

    @Override
    protected void onProgressUpdate(Room... values) {
        super.onProgressUpdate(values);

    }

    @Override
    protected void onPostExecute(Void aVoid) {
        cpi.send(list);

        super.onPostExecute(aVoid);
    }
    public interface Cpi {
        void send(List<Room> data);
    }
}
